package main

import (
	"fmt"
	"gin-excel/models"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"net/http"
)

func main() {
	router := gin.Default() // 默认包含Logger和Recovery中间件
	// json 返回
	router.GET("/ping", func(c *gin.Context) {
		// query 参数
		id := c.Query("id")
		name := c.DefaultQuery("name", "Mike")
		fmt.Println(id, name)

		c.JSON(http.StatusOK, gin.H{
			"message": "pong"})
	})

	// 文件上传
	router.POST("/upload", func(c *gin.Context) {
		form, _ := c.MultipartForm()
		files := form.File["upload"]
		guid := uuid.New().String()
		filePath := "static/uploadfile/" + guid + ".xlsx"
		for _, file := range files {
			fmt.Println(file.Filename)
			_ = c.SaveUploadedFile(file, filePath)
		}
		if filePath == "" {
			c.JSON(400, gin.H{
				"msg":  "没有文件",
				"code": 400,
			})
		}

		err := models.ImportUsers(filePath)
		if nil == err {
			c.JSON(200, gin.H{
				"message": err.Error(),
			})
		} else {
			c.JSON(200, gin.H{
				"message": "success",
			})
		}

		c.JSON(200, gin.H{
			"message": "success",
		})
	})

	router.Run(":8080") // 默认监听端口号:8080

}
